// pages/card-active/card-active.js
var request = require('../../utils/request.js');
var config = require('../../config.js');
var app = getApp();
var timer = -1;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      mobile: '',
      auth: '',
      isSending: 0,
      authTime: 60
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var mobile = wx.getStorageSync('mobile');
      if(mobile){
          wx.switchTab({
              url: '/pages/index/index'
          })
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
      var that = this;

  },
  init() {
      new app.WeToast();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  authBtnClick(e) {
      var dataset = e.currentTarget.dataset;
      var authTime = dataset.authTime;
      var mobile = dataset.mobile;
      var that = this;

      if (!mobile) {
          wx.showToast({
            icon: 'none',
            title: '手机号码必填'
          })
          return false;
      }

      that.setData({
        'form.isSending': 1,
        'form.authTime': 60
      });

      request.get({ path: config.sendMsg }, { mobile: mobile }).then(function(res) {
        var authData = res.data;
        console.log('这里是发验证码的接口数据',res)
        if (authData.ret == 0) {
            timer = setInterval(function() {
                if (that.data.form['authTime'] > 0) {
                    that.data.form['authTime'] -= 1;
                    that.setData({
                      'form.authTime': that.data.form['authTime']
                    })
                } else {
                    clearInterval(timer);
                    that.setData({
                      'form.isSending': 0
                    })
                }
            }, 1000)
        }
    }, function(res) {
        clearInterval(timer);
        that.setData({
            'form.isSending': 0
        })
        wx.showToast({
            icon: 'none',
            title: res.data.msg
        })
    })
  },
  bindChange(e) {
    var name = e.currentTarget.dataset.name;
    this.setData({
      [`form.${name}`]: e.detail.value
    })
  },
  formSubmit(e){
    var that = this;
    var data = e.detail.value;

    if(data['mobile'] == ''){
      wx.showToast({
        icon: 'none',
        title: '电话号码必填'
      })
      return false;
    }

    if(data['auth'] == ''){
      wx.showToast({
        icon: 'none',
        title: '验证码必填'
      })
      return false;
    }
    
    request.get({'path': config.userLogin}, data).then(function(res){
      var ratingData = res.data;
        console.log('这里是手机号提交后的数据',res)
        if (ratingData.ret == 0) {
        wx.setStorageSync('mobile', ratingData.data.mobile);
        //登陆跳转到首页
          wx.switchTab({
          url: '/pages/index/index'
        })

      } else {
        wx.showToast({
          icon: 'none',
          title: ratingData.msg
        })
        return false;
      }
    }, function(res){
      wx.showToast({
        icon: 'none',
        title: res.data.msg
      })
      return false;
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '微卡拉智能工单系统',
      path: '/page/login/login'
    }
  }
})